webhook error messages and status codes

James Peret 7 years ago
parent
commit
79d2699a19
3 changed files with 30 additions and 16 deletions
  1. 1 1
      package.json
  2. 3 1
      readme.md
  3. 26 14
      src/multi-adapter.coffee

+ 1 - 1
package.json

@@ -1,6 +1,6 @@
1 1
 {
2 2
   "name": "hubot-multi-adapter",
3
-  "version": "0.3.1",
3
+  "version": "0.3.2",
4 4
   "description": "A Hubot adapter that can receive messages thru socket.io or telegram",
5 5
   "main": "src/multi-adapter",
6 6
   "scripts": {

+ 3 - 1
readme.md

@@ -36,8 +36,10 @@ You can send **webhooks** thru this adapter using the endpoint ```/webhook```. M
36 36
   "first_name" : "John",
37 37
   "last_name" : "Doe",
38 38
   "room" : 455098,
39
-  "service" : "webhook",
39
+  "username" : "jamesperet"
40
+  "service" : "telegram",
40 41
   "command" : "example-command"
42
+  "msg_type" : "command"
41 43
 }
42 44
 ```
43 45
 

+ 26 - 14
src/multi-adapter.coffee

@@ -80,26 +80,38 @@ class MultiAdapter extends Adapter
80 80
       user.last_name = req.body['message[from][last_name]']
81 81
       user.username = req.body['message[from][username]']
82 82
       user.room = chat_id
83
+      user.msg_type = "message"
83 84
       @receive new TextMessage user, text
84 85
       res.end()
85 86
 
86 87
     # General Webhook
87 88
     app.post '/webhook', (req, res) =>
88 89
       console.log(req.body)
89
-      chat_id = req.body.user.room
90
-      # Get username
91
-      user_name = req.body.user.first_name + " " + req.body.user.last_name
92
-      command = req.body.command
93
-      @robot.brain.set 'log_id_' + chat_id, new Date().getUTCMilliseconds();
94
-      user = @userForId chat_id, name: user_name, room: chat_id
95
-      console.log("Webhook received from " + user_name + " with command:" )
96
-      console.log(command)
97
-      user.service = "webhook"
98
-      user.first_name = req.body.user.first_name
99
-      user.last_name = req.body.user.last_name
100
-      user.username = req.body.user.username
101
-      user.room = chat_id
102
-      @receive new TextMessage user, text
90
+      if(req.body.user != undefined){
91
+        if(req.body.user.room && req.body.user.service && req.body.user.first_name && req.body.user.last_name && req.body.user.username && req.body.user.msg_type){
92
+          chat_id = req.body.user.room
93
+          # Get username
94
+          user_name = req.body.user.first_name + " " + req.body.user.last_name
95
+          command = req.body.command
96
+          @robot.brain.set 'log_id_' + chat_id, new Date().getUTCMilliseconds();
97
+          user = @userForId chat_id, name: user_name, room: chat_id
98
+          console.log("Webhook received from " + user_name + " with command:" )
99
+          console.log(command)
100
+          user.service = req.body.user.service
101
+          user.first_name = req.body.user.first_name
102
+          user.last_name = req.body.user.last_name
103
+          user.username = req.body.user.username
104
+          user.room = chat_id
105
+          user.msg_type = req.body.user.msg_type
106
+          @receive new TextMessage user, text
107
+          res.send({"message" : "received"})
108
+        } else {
109
+          res.send({"message" : "The user object has mising properties. Follow instruction on https://github.com/jamesperet/hubot-multi-adaptor"})
110
+        }
111
+      } else {
112
+        res.send({"message" : "Please check the body of your request. Follow instruction on https://github.com/jamesperet/hubot-multi-adaptor"})
113
+      }
114
+
103 115
       res.end()
104 116
 
105 117
     @emit 'connected'